#include <cmath>
#include <cstdio>
#include <unordered_map>
#define ri register int
using namespace std;

int a, b, p;
typedef long long ll;
unordered_map<int, int> has;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

inline ll qpow(ll base, int index, int mod) {
  ll res = 1;
  while (index) {
    if (index & 1) res = res * base % mod;
    base = base * base % mod;
    index >>= 1;
  }
  return res;
}

void exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= a / b * x;
}

inline int bsgs(int a, int b, int p, ll c) {
  has.clear();
  int blo = (int)sqrt(p) + 1;
  ll x, y, q;
  exgcd(c, p, x, y), b = (b * x % p + p) % p;
  q = qpow(a, blo, p), exgcd(q, p, x, y), q = (x % p + p) % p;
  for (ri i = 1, j = 0; j <= blo; ++j, i = 1ll * i * a % p) {
    if (!has.count(i)) has[i] = j;
  }
  for (ri i = b, j = 0; j <= blo; ++j, i = 1ll * i * q % p) {
    if (has[i]) return j * blo + has[i];
  }
  return -1;
}

inline int exbsgs(int a, int b, int p) {
  a %= p, b %= p;
  int div, k = 0;
  ll c = 1;
  if (b == 1) return 0;
  while ((div = gcd(a, p)) != 1) {
    if (b % div) return -1;
    b /= div, p /= div;
    ++k, c = c * (a / div) % p;
    if (c == b) return k;
  }
  return ((div = bsgs(a, b, p, c)) == -1 ? -1 : div + k);  //这里用div是省了一个变量
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
#endif
  while (true) {
    scanf("%d%d%d", &a, &p, &b);
    if (a == 0 && p == 0 && b == 0) break;
    int ans = exbsgs(a, b, p);
    if (~ans)
      printf("%d\n", ans);
    else
      printf("No Solution\n");
  }
  return 0;
}